﻿using DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAO
{
    public class MachSacDao
    {
        public int ThemMachSac(ThongSoChungDTO ts, ThietBiDienDTO tb)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlCommand cmd = new SqlCommand("THEM_MACHSAC", _cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@MaTB", SqlDbType.Char, 20));
            cmd.Parameters.Add(new SqlParameter("@TenTB", SqlDbType.NVarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@NgayNhap", SqlDbType.DateTime));
            cmd.Parameters.Add(new SqlParameter("@CSKhaDung", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("@DonGia", SqlDbType.Int));

            cmd.Parameters["@MaTB"].Value = ts.MATB;
            cmd.Parameters["@TenTB"].Value = ts.TENTB;
            cmd.Parameters["@NgayNhap"].Value = ts.NGAYNHAP;
            cmd.Parameters["@CSKhaDung"].Value = ts.CSKHADUNG;
            cmd.Parameters["@DonGia"].Value = ts.DONGIA;

            cmd.Parameters.Add(new SqlParameter("@Vol", SqlDbType.Float));
            cmd.Parameters.Add(new SqlParameter("@Amp", SqlDbType.Decimal));

            cmd.Parameters.Add(new SqlParameter("@GhiChu", SqlDbType.NVarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@LoaiTB", SqlDbType.Int));

            cmd.Parameters["@Vol"].Value = tb.VOL;
            cmd.Parameters["@Amp"].Value = tb.AMP;
            cmd.Parameters["@GhiChu"].Value = tb.GHICHU;
            cmd.Parameters["@LoaiTB"].Value = tb.LOAITB;

            int _kq = cmd.ExecuteNonQuery();
            _cn.Close();
            return _kq;
        }


        public int SuaMachSac(ThongSoChungDTO ts, ThietBiDienDTO tb)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlCommand cmd = new SqlCommand("SUAMACHSAC", _cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@STT", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("@MaTB", SqlDbType.Char, 20));
            cmd.Parameters.Add(new SqlParameter("@TenTB", SqlDbType.NVarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@NgayNhap", SqlDbType.DateTime));
            cmd.Parameters.Add(new SqlParameter("@CSKhaDung", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("@DonGia", SqlDbType.Int));

            cmd.Parameters["@STT"].Value = tb.STT;
            cmd.Parameters["@MaTB"].Value = ts.MATB;
            cmd.Parameters["@TenTB"].Value = ts.TENTB;
            cmd.Parameters["@NgayNhap"].Value = ts.NGAYNHAP;
            cmd.Parameters["@CSKhaDung"].Value = ts.CSKHADUNG;
            cmd.Parameters["@DonGia"].Value = ts.DONGIA;

            cmd.Parameters.Add(new SqlParameter("@Vol", SqlDbType.Float));
            cmd.Parameters.Add(new SqlParameter("@Amp", SqlDbType.Decimal));

            cmd.Parameters.Add(new SqlParameter("@GhiChu", SqlDbType.NVarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@LoaiTB", SqlDbType.Int));

            cmd.Parameters["@Vol"].Value = tb.VOL;
            cmd.Parameters["@Amp"].Value = tb.AMP;
            cmd.Parameters["@GhiChu"].Value = tb.GHICHU;
            cmd.Parameters["@LoaiTB"].Value = tb.LOAITB;

            int _kq = cmd.ExecuteNonQuery();
            _cn.Close();
            return _kq;
        }


        public int KiemTra_MaTB(ThongSoChungDTO ts)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlCommand cmd = new SqlCommand("MaMS_KT", _cn);
            cmd.CommandType = CommandType.StoredProcedure;


            cmd.Parameters.Add(new SqlParameter("@MaTB", SqlDbType.Char, 20));
            cmd.Parameters.Add(new SqlParameter("@TonTai", SqlDbType.Int));
            cmd.Parameters["@TonTai"].Direction = ParameterDirection.Output;


            cmd.Parameters["@MaTB"].Value = ts.MATB;
            cmd.Parameters["@TonTai"].Value = ts.MATS;

            cmd.ExecuteNonQuery();
            int _kq = (int)cmd.Parameters["@TonTai"].Value;
            _cn.Close();
            return _kq;
        }

        public int KT_TheoTenMachSac(ThongSoChungDTO ts)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlCommand cmd = new SqlCommand("KT_TheoTenMachSac", _cn);
            cmd.CommandType = CommandType.StoredProcedure;


            cmd.Parameters.Add(new SqlParameter("@TenTB", SqlDbType.NVarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@TonTai", SqlDbType.Int));
            cmd.Parameters["@TonTai"].Direction = ParameterDirection.Output;


            cmd.Parameters["@TenTB"].Value = ts.TENTB;
            //cmd.Parameters["@TonTai"].Value = ts.MATS;

            cmd.ExecuteNonQuery();
            int _kq = (int)cmd.Parameters["@TonTai"].Value;
            _cn.Close();
            return _kq;
        }

        
        public int XoaMachSac(int STT)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlCommand cmd = new SqlCommand("XOA_MACHSAC", _cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@STT", SqlDbType.Int));
            cmd.Parameters["@STT"].Value = STT;
            int _kq = cmd.ExecuteNonQuery();
            _cn.Close();
            return _kq;
        }
    }
}
